package com.paobuji.aidebao.myapplication.ui;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jaeger.library.StatusBarUtil;
import com.paobuji.aidebao.myapplication.R;
import com.paobuji.aidebao.myapplication.activity.BaseActivity;
import com.paobuji.aidebao.myapplication.bean.MyPlanCacheBean;
import com.paobuji.aidebao.myapplication.tool.ACache;
import com.paobuji.aidebao.myapplication.tool.DateUtil;
import com.paobuji.aidebao.myapplication.view.CustomTitlebar;
import com.paobuji.aidebao.myapplication.view.RectSeekBar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MySportPlanSetActivity extends BaseActivity {

    @BindView(R.id.mainTitle)
    CustomTitlebar mainTitle;
    @BindView(R.id.myPlant)
    TextView myPlant;
    @BindView(R.id.circle_seekbar)
    RectSeekBar circleSeekbar;
    @BindView(R.id.week)
    FloatingActionButton week;
    @BindView(R.id.moth)
    FloatingActionButton moth;
    @BindView(R.id.startDate)
    TextView startDate;
    @BindView(R.id.endDate)
    TextView endDate;

    public int pro;
    MyPlanCacheBean myCacheBean;
    ACache aCache;
    Context context;
    @BindView(R.id.monthText)
    TextView monthText;
    @BindView(R.id.weekText)
    TextView weekText;

    DateUtil dateUtil;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_sport_plan_set);
        ButterKnife.bind(this);
        //设置全透明
        StatusBarUtil.setTransparent(this);
        context = getApplicationContext();
        dateUtil = new DateUtil();

        aCache = ACache.get(context);
        startDate.setText(DateUtil.getSysDate(0));
        endDate.setText(DateUtil.getSysDate(7));

        myCacheBean = (MyPlanCacheBean) aCache.getAsObject("myCacheBean");
        if(myCacheBean == null){
            myCacheBean = new MyPlanCacheBean();


        }else{
            //dateUtil.differenceDate(myCacheBean.getStopTime(),myCacheBean.getStartTime());
            int n = dateUtil.differenceDate(myCacheBean.getStopTime(),myCacheBean.getStartTime());
            if(n == 7){


                circleSeekbar.setmCurrentProgress((int) myCacheBean.getPlan());
                endDate.setText(myCacheBean.getStopTime());
                monthText.setTextColor(getResources().getColor(R.color.textColor));
                weekText.setTextColor(getResources().getColor(R.color.weekColor));
            }else{
                circleSeekbar.setmCurrentProgress((int) myCacheBean.getPlan());
                endDate.setText(myCacheBean.getStopTime());
                monthText.setTextColor(getResources().getColor(R.color.montehColor));
                weekText.setTextColor(getResources().getColor(R.color.textColor));
            }
        }



        Log.e("11111", "onCreate:11111111111 ");

        //弧形滑块的监听
        circleSeekbar.setmOnSeekBarChangeListener(new RectSeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(int progress) {
                pro = progress;
            }

            //开始按下去
            @Override
            public void onStartTrackingTouch() {

            }

            //滑动结束
            @Override
            public void onStopTrackingTouch() {
                //Toast.makeText(MySportPlanSetActivity.this, pro + "", Toast.LENGTH_SHORT).show();
                myCacheBean.setPlan(circleSeekbar.getmCurrentProgress());
            }
        });

        //标题栏事件监测
        mainTitle.setAction(new CustomTitlebar.TitleBarOnClickListener() {
            @Override
            public void performAction(View view) {
                switch (view.getId()) {
                    case R.id.iv_left:
                        finish();
                        //Toast.makeText(MainActivity.this, "左边图片按钮", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.tv_left:
                        finish();
                        //Toast.makeText(MainActivity.this, "右边文字按钮", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.iv_right:
                        savePlan();
                        //Toast.makeText(MainActivity.this, "右边文字按钮", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.tv_right:
                        savePlan();
                        //Toast.makeText(MainActivity.this, "右边文字按钮", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });

    }

    @Override
    protected void onStart() {
        super.onStart();


    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {

    }

    @OnClick({R.id.week, R.id.moth})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.week:
                Log.e("11111", "onClick: 1111" );
                startDate.setText(DateUtil.getSysDate(0));
                endDate.setText(DateUtil.getSysDate(7));
                circleSeekbar.setmSeekBarMax(100);
                myCacheBean.setStartTime(DateUtil.getSysDate(0));
                myCacheBean.setStopTime(DateUtil.getSysDate(7));
                myCacheBean.setPlan(circleSeekbar.getmCurrentProgress());
                monthText.setTextColor(getResources().getColor(R.color.textColor));
                weekText.setTextColor(getResources().getColor(R.color.weekColor));
                break;
            case R.id.moth:
                startDate.setText(DateUtil.getSysDate(0));
                endDate.setText(DateUtil.getSysDate(30));
                circleSeekbar.setmSeekBarMax(600);
                myCacheBean.setStartTime(DateUtil.getSysDate(0));
                myCacheBean.setStopTime(DateUtil.getSysDate(30));
                myCacheBean.setPlan(circleSeekbar.getmCurrentProgress());
                weekText.setTextColor(getResources().getColor(R.color.textColor));
                monthText.setTextColor(getResources().getColor(R.color.montehColor));
                break;
        }
    }

    public void savePlan() {
        aCache.put("myCacheBean", myCacheBean);

        Log.e("11111", "initData: " +  myCacheBean.getPlan());

        Gson gson = new Gson();
        String s = gson.toJson(myCacheBean);
        Log.e("11111", "initData: "+ s);

        finish();
    }
}
